package com.laob.laob_special.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.laob.laob_special.service.HttpService;
import com.laob.laob_special.util.HttpClientUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @author zhangh     
 * @version V1.0.0
 * @projectName laob_special
 * @title     LaoBController   
 * @package    com.laob.laob_special.controller  
 * @date   2019/9/9 13:46  
 * @explain
 */
@RequestMapping("/laoB")
@RestController
public class LaoBController {

    @Autowired
    private HttpService httpService;


    /**
     * 扫描可访问端口
     * @param httpCondition
     * @return
     */
    @RequestMapping("/testPorts")
    public List<Integer> testPorts(String httpCondition) {
        System.out.println(httpCondition);
        JSONObject requsetParam = JSONObject.parseObject(httpCondition);
        // 请求的方法
        String method = requsetParam.getString("method");
        if (null ==  method){
            method = "";
        }
        String type = requsetParam.getString("type");
        String ipAddress = requsetParam.getString("ipAddress");
        Integer minPort = requsetParam.getInteger("minPort");
        Integer maxPort = requsetParam.getInteger("maxPort");

        long start = new Date().getTime();
        List<String> realPorts = new LinkedList<>();

        List<Integer> integers = httpService.testPort(ipAddress, type, minPort, maxPort, method);

        long time = new Date().getTime() - start;
        System.out.println("共用时:"+(time)+"毫秒");
        if (integers != null){
            System.out.println("可用端口号:"+integers);
        }else {
            System.out.println("未找到可用端口!");
        }
        return integers;
    }


    /**
     * 测试接口
     * @param httpCondition
     * @return
     */
    @RequestMapping("/sendMsg")
    public JSONObject sendMsg(String httpCondition) {
        System.out.println(httpCondition);
        JSONObject requsetParam = JSONObject.parseObject(httpCondition);
        // 请求的方法
        String method = requsetParam.getString("method");
        String reqContent = requsetParam.getString("reqContent");
        if (null ==  method){
            method = "";
        }
        if (null ==  reqContent){
            reqContent = "";
        }
        String type = requsetParam.getString("type");
        String ipAddress = requsetParam.getString("ipAddress");

        long start = new Date().getTime();
        List<String> realPorts = new LinkedList<>();

        JSONObject jsonObject = httpService.sendMsg(ipAddress+method, type,reqContent);

        long time = new Date().getTime() - start;
        System.out.println("共用时:"+(time)+"毫秒");
        System.out.println(jsonObject);

        if (null == jsonObject || jsonObject.isEmpty()){
            Map<String, Object> map = new HashMap<>();
            map.put("异常","未接收到任何数据返回");
            jsonObject = new JSONObject(map);
        }
        return jsonObject;
    }

    /**
     * 停止扫描
     * @return
     */
    @RequestMapping("/stopScan")
    public void stopScan() {
        httpService.stopScan();
    }

    /**
     * 停止扫描
     * @return
     */
    @RequestMapping("/sendMsgByCustomHeader")
    public JSONObject sendMsgByCustomHeader(HttpServletRequest request,String data)  throws Exception{

        // 获取请求参数
        JSONObject dataJson = JSONObject.parseObject(data);
        String reqHeaderStr = dataJson.getString("reqHeaders");
        String type = dataJson.getString("type");
        String ip = dataJson.getString("ip");
        // 请求并获取结果
        JSONObject jsonObject = httpService.httpGetByCustomerHeader(type, ip,reqHeaderStr);
        if (jsonObject == null){
            jsonObject = new JSONObject();
            jsonObject.put("content","没有得到响应数据");
        }
        return jsonObject;
    }

}
